package com.demo.framework.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 *  
* @ClassName: SignView 
* @Description: TODO(签名用到的画板) 
* @author 谢云腾
* @date 2013-3-22 下午02:53:38 
* @version 1.0
 */
public class SignView extends View {
    private static int SCREEN_WIDTH;
    private static int SCREEN_HEIGHT;
    
    private float mX, mY;
    private Path mPath;
    private Paint mPaint;

    public Paint getmPaint() {
        return mPaint;
    }

    public void setmPaint(Paint mPaint) {
        this.mPaint = mPaint;
    }

    public Canvas getmCanvas() {
        return mCanvas;
    }

    public Paint getmBitmapPaint() {
        return mBitmapPaint;
    }

    private static final float TOUCH_TOLERANCE = 4;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Paint mBitmapPaint;

    public Bitmap getmBitmap() {
        return mBitmap;
    }

    public void setmBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
    }

    public void setmBitmapPaint(Paint mBitmapPaint) {
        this.mBitmapPaint = mBitmapPaint;
    }

    public void setmCanvas(Canvas mCanvas) {
        this.mCanvas = mCanvas;
    }

    public SignView(Context c) {
        super(c);

        mPaint = new Paint();// 创建画笔渲染对象
        mPaint.setAntiAlias(true);// 设置抗锯齿，让绘画比较平滑
        mPaint.setDither(true);// 设置递色
        mPaint.setColor(Color.BLACK);// 设置画笔的颜色
        mPaint.setStyle(Paint.Style.STROKE);// 画笔的类型有三种（1.FILL 2.FILL_AND_STROKE
                                            // 3.STROKE ）
        mPaint.setStrokeJoin(Paint.Join.ROUND);// 默认类型是MITER（1.BEVEL 2.MITER
                                                // 3.ROUND ）
        mPaint.setStrokeCap(Paint.Cap.ROUND);// 默认类型是BUTT（1.BUTT 2.ROUND
                                                // 3.SQUARE ）
        mPaint.setStrokeWidth(3);// 设置描边的宽度，如果设置的值为0那么边是一条极细的线

        setScreenDensity(c);
        mBitmap = Bitmap.createBitmap(SCREEN_WIDTH, SCREEN_HEIGHT, Bitmap.Config.ARGB_8888);
        //mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);// 绘制固定大小的bitmap对象
                                                                            // 320
        mCanvas = new Canvas(mBitmap);// 将固定的bitmap对象嵌入到canvas对象中
        mPath = new Path();// 创建画笔路径
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }
    
    private void setScreenDensity(Context c) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity)c).getWindowManager().getDefaultDisplay().getMetrics(dm);
        int widthPixels= dm.widthPixels;
        int heightPixels= dm.heightPixels;
        float density = dm.density;
        SCREEN_WIDTH = new Float(widthPixels*density).intValue();
        SCREEN_HEIGHT = new Float(heightPixels*density).intValue();
        Log.i("DemoPath", "width = " + SCREEN_WIDTH + ", hight = " + SCREEN_HEIGHT);
    }

    public void reset() {
        mBitmap = Bitmap.createBitmap(SCREEN_WIDTH, SCREEN_HEIGHT, Bitmap.Config.ARGB_8888);
        //mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);// 绘制固定大小的bitmap对象
        mCanvas = new Canvas(mBitmap);// 将固定的bitmap对象嵌入到canvas对象中
        invalidate();
    }

    public Path getmPath() {
        return mPath;
    }

    public void setmPath(Path mPath) {
        this.mPath = mPath;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFffffff);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
        super.onDraw(canvas);
    }

    private void onTouchDown(float x, float y) {
        mPath.reset();// 将上次的路径保存起来，并重置新的路径。
        mPath.moveTo(x, y);// 设置新的路径“轮廓”的开始
        mX = x;
        mY = y;
    }

    private void onTouchMove(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
    }

    private void onTouchUp(float x, float y) {
        mPath.lineTo(mX, mY);// 从最后一个指定的xy点绘制一条线，如果没有用moveTo方法，那么起始点表示（0，0）点。
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);// 手指离开屏幕后，绘制创建的“所有”路径。
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        mListener.onTouch();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:// 手指开始按压屏幕，这个动作包含了初始化位置
            onTouchDown(x, y);
            invalidate();// 刷新画布，重新运行onDraw（）方法
            break;
        case MotionEvent.ACTION_MOVE:// 手指按压屏幕时，位置的改变触发，这个方法在ACTION_DOWN和ACTION_UP之间。
            onTouchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:// 手指离开屏幕，不再按压屏幕
            onTouchUp(x, y);
            invalidate();
            break;
        default:
            break;
        }
        return true;
    }

    public void clearData() {
        try {
            if (mBitmap != null)
                mBitmap.recycle();
        } catch (Exception e) {
            // TODO: handle exception
        } finally {
            mBitmap = null;
        }
    }

    private OnNewTouchListener mListener;

    public interface OnNewTouchListener {

        void onTouch();
    }

    public void setOnNewTouchListener(OnNewTouchListener listener) {
        mListener = listener;
    }
}
